import axios, { AxiosInstance } from "axios";
import { Agent } from "https";
import axiosRetry from "axios-retry";

const hydraPublicApiBase = process.env.HYDRA_PUBLIC_API_BASE || "http://127.0.0.1";
const hydraPrivateApiBase = process.env.HYDRA_PRIVATE_API_BASE || "http://127.0.0.1";
const eacpPublicApiBase = process.env.EACP_PUBLIC_API_BASE || "http://127.0.0.1";
const eacpPrivateApiBase = process.env.EACP_PRIVATE_API_BASE || "http://127.0.0.1";
const authenticationPublicApiBase = process.env.AUTHENTICATION_PUBLIC_API_BASE || "http://127.0.0.1";
const authenticationPrivateApiBase = process.env.AUTHENTICATION_PRIVATE_API_BASE || "http://127.0.0.1";
const timeoutLabel = "timeout";
const retryCount = 2;
const eacpApiTimeout = 3000;

export const hydraPublicApi = axios.create({
    baseURL: hydraPublicApiBase,
    headers: {
        "Content-Type": "application/json",
    },
    timeout: 5000,
    httpsAgent: new Agent({ rejectUnauthorized: false }),
});
export const hydraPrivateApi = axios.create({
    baseURL: hydraPrivateApiBase,
    headers: {
        "Content-Type": "application/json",
    },
    timeout: 5000,
});

export const eacpPublicApi = axios.create({
    baseURL: eacpPublicApiBase,
    headers: {
        "Content-Type": "application/json",
    },
    timeout: eacpApiTimeout,
    httpsAgent: new Agent({ rejectUnauthorized: false }),
});
export const eacpPrivateApi = axios.create({
    baseURL: eacpPrivateApiBase,
    headers: {
        "Content-Type": "application/json",
    },
    timeout: eacpApiTimeout,
});

export const authenticationPublicApi = axios.create({
    baseURL: authenticationPublicApiBase,
    headers: {
        "Content-Type": "application/json",
    },
    timeout: 5000,
    httpsAgent: new Agent({ rejectUnauthorized: false }),
});
export const authenticationPrivateApi = axios.create({
    baseURL: authenticationPrivateApiBase,
    headers: {
        "Content-Type": "application/json",
    },
    timeout: 5000,
});
enum Service {
    UNKNOWN = "unknown",
    EACP = "eacp",
    HYDRA = "hydra",
    AUTHENTICATION = "authentication",
}
export function getServiceNameFromApi(api: string = ""): Service {
    switch (true) {
        case api.includes(`/${Service.EACP}/`):
            return Service.EACP;
        case api.includes(`/${Service.HYDRA}/`):
            return Service.HYDRA;
        case api.includes(`/${Service.AUTHENTICATION}/`):
            return Service.AUTHENTICATION;
        default:
            return Service.UNKNOWN;
    }
}
export function getErrorCodeFromService(service: Service): number {
    switch (service) {
        case Service.EACP:
            return 500041001;
        case Service.HYDRA:
            return 500041002;
        case Service.AUTHENTICATION:
            return 500041003;
        default:
            return 500000000;
    }
}

function retry(service: AxiosInstance) {
    axiosRetry(service, {
        // 设置自动发送请求次数
        retries: retryCount,
        // 重复请求延迟（毫秒）
        retryDelay: () => 0,
        //  重置超时时间
        shouldResetTimeout: true,
        //控制是否应该重试请求的回调，true为打开自动发送请求，false为关闭自动发送请求
        retryCondition: (error) => error.message.includes(timeoutLabel),
    });
}

retry(eacpPublicApi);
retry(eacpPrivateApi);
